// subpages/secretRecharge/secretRecharge.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    secretNum:'',
    accountNum:'',
    showLoading:false,
    noMoretip:false,
    page:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestRecords();
  },
  //填写卡密
  secretChange(e){
    console.log(e);
    this.setData({
      secretNum:e.detail.value
    })
  },
  accountChange(e) {
    console.log(e);
    this.setData({
      accountNum: e.detail.value
    })
  },
  //确认充值
  secretRecharge(){
    var that = this;
    if (!this.data.secretNum){
      wx.showToast({
        title: '请输入充值卡密',
        icon:'none'
      })
      return
    }
    if (!this.data.accountNum) {
      wx.showToast({
        title: '请输入充值卡号',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '确认充值？',
      content: '',
      success(res){
        if(res.confirm){
          
          wx.showLoading({
            title: '加载中',
            mask: true
          })
          var data = {};
          data.map = 'applet_cardpwd_recharge';
          data.card = that.data.accountNum;
          data.pwd = that.data.secretNum;
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              console.log(data)
              console.log(res.data);
              if (res.data.ec == 200) {
                wx.showToast({
                  title: '充值成功！',
                })
                setTimeout(function(){
                  wx.navigateBack({
                    delta:1
                  })
                },1500)
              } else {
                wx.showToast({
                  title: res.data.em,
                  icon: 'none'
                })
              }
            },
            fail: function () {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              })
            },
            complete: function () {
              setTimeout(function () {
                wx.hideLoading()
              }, 1500)
            }
          });
        }
      }
    })
  },
  requestRecords: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_cardpwd_recharge_list';
    data.page = page;
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        // console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.recordsList;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            recordsList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.recordsList);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              recordsList: []
            })
          }
          that.setData({
            noMoretip: true,
            showLoading: false
          });
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    this.requestRecords();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestRecords();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})